// 导入
import Store from '@/store'
import router from '@/router'
// 导入
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'
const whitePath = ['/login', '/404']
// 路由前置守卫
router.beforeEach(async(to, from, next) => {
  nProgress.start()
  if (Store.state.user.token) {
    if (to.path.toLowerCase() === '/login') {
      next('/')
    } else {
      if (Store.state.user.userInfo.userId) {
        next()
      } else {
        await Store.dispatch('user/getuserInfo')
        next(to.fullPath)
      }
    }
  } else {
    //   如以上都不是,则否,查看白名单
    if (whitePath.includes(to.path)) {
      next()
    } else {
      // 这里一样,那个界面退出,登录就到那个界面:就是!!!从那个页面退出的,登录进去就到那个界面
      next('/login?req=' + to.fullPath)
    }
  }
  nProgress.done()
})
router.afterEach(() => {
  nProgress.done()
})
